<?php
namespace app\index\controller;
use think\Controller;
class Renter extends Controller{
	public $info;
	public $vill;
	//租房搜索页
	public function select(){
		if(request()->isPost()){
			//模糊搜索
			$where = input('post.av');
			$info = model('village')
					->alias('v')
					->join('pub p','v.id=p.vill && v.name="'.$where.'" || v.id=p.area && v.name="'.$where.'"')
					->select();
			$data = [];
			foreach($info as $k=>$v){
				$data[] = $v->toArray();
				if($data[$k]['state'] == '合租'){
					$he = model('hezu')->where('rid='.$data[$k]['rid'])->select();
					$hezu = [];
					foreach($he as $kk=>$v){
						$hezu[] = $v->toArray();
						$data[$k]['hid'][] = $hezu[$kk]['hid'];
					}
					//dump($hezu);exit;
				}
			}
		}else{
			$info = model('pub')->where('ispub=1')->select();
			$data = [];
			foreach($info as $k=>$v){
				$data[] = $v->toArray();
				//dump($data[$k]['state']);exit;
				if($data[$k]['state'] == '合租'){
					$he = model('hezu')->where('rid='.$data[$k]['rid'])->select();
					$hezu = [];
					foreach($he as $kk=>$v){
						$hezu[] = $v->toArray();
						$data[$k]['hid'][] = $hezu[$kk]['hid'];
					}
					//dump($hezu);exit;
				}
			}
		}
		$this->assign('data',$data);
	    return $this->fetch();
		
	}
	
	//条件搜索
	public function selected(){
		//区域
		$where = ' like '.input('get.type').' && ispub=1';
		//价格
		switch(input('get.price')){
			case 'lt1500':
				$where = 'price<1500';
				break;
			case 'lt2000':
				$where = 'price>1500 && price<=2000';
				break;
			case 'lt3000':
				$where = 'price>=3000';
				break;
		}
		if(input('get.price')){
			$info = model('pub')->where($where)->select();
		}elseif(input('get.type')){
			//区域
			$where = 'ispub=1 && desc2 like "%'.input('get.type').'%"';
			$info = model('pub')->where($where)->select();
		}elseif(input('get.state')){
			//整租合租
			$where = 'state="'.input('get.state').'" && ispub=1';
			$info = model('pub')->where($where)->select();
			$data = [];
			foreach($info as $k=>$v){
				$data[] = $v->toArray();
				$he = model('hezu')->where('rid='.$data[$k]['rid'])->select();
				$hezu = [];
				foreach($he as $kk=>$v){
					$hezu[] = $v->toArray();
					$data[$k]['hid'][] = $hezu[$kk]['hid'];
				}
			}
			$this->assign('he',$he);
			$this->assign('data',$data);
			return $this->fetch('select');
		}elseif(input('get.mian')){
			//面积
			switch(input('get.mian')){
				case 'lt15':
					$where = 'mian<15 && ispub=1';
					break;
				case 'lt30':
					$where = 'mian>15 && mian<=30 && ispub=1';
					break;
				case 'lt50':
					$where = 'mian>=50 && ispub=1';
					break;
			}
			$info = model('pub')->where($where)->select();
		}else{
			$info = model('pub')->where(input('get.'))->select();
		}
		// $data = [];
		// foreach($info as $v){
		// 	$data[] = $v->toArray();
		// }
		$data = [];
			foreach($info as $k=>$v){
				$data[] = $v->toArray();
				$he = model('hezu')->where('rid='.$data[$k]['rid'])->select();
				$hezu = [];
				foreach($he as $kk=>$v){
					$hezu[] = $v->toArray();
					$data[$k]['hid'][] = $hezu[$kk]['hid'];
				}
			}
		$this->assign('data',$data);
	    return $this->fetch('select');
	}

	  //详情页
	public function details(){
		$info = model('pub')
		->alias('p')
		->field('m.thumb head,uname,utel,title,mname,price,floor,allfloor,mian,face,p.area area,vill,addr,desc1,desc2,desc3,img,p.thumb pthumb,state,character,ini')
		->join('member m','p.mname=m.uname && rid='.input('get.rid'))
		->find()
		->toArray();
		$this->info = $info;
		$area = model('village')->where('id='.$info['area'])->find()->name;
		$vill = model('village')->where('id='.$info['vill'])->find()->name;
		$this->vill = $vill;
		$info['area'] = $area;
		$info['vill'] = $vill;
		$imgs = json_decode($info['img'],true);

		$character = explode(',',$info['character']);
		$ini = explode(',',$info['ini']);
		$char = [];
		foreach($character as $v){
			$char[] = model('OwnerCharacter')->where('id='.$v)->find()->info;
		}
		$init = [];
		foreach($ini as $v){
			$data = model('OwnerIni')->where('id='.$v)->find();
			$in = $data->info;
			$thumb = $data->thumb;
			$init[$in] = $thumb;
		}
		//合租
		if($info['state'] == '合租'){
			$hezu = model('hezu')->where('rid='.input('get.rid'))->order('hid')->select();
			$he = [];
			foreach($hezu as $v){
				$he[] = $v->toArray();
			}
			$this->assign('he',$he);
		}
		//dump($init);exit;
		//执行推荐房源
		$tuijian = $this->tuijian();
		$this->assign('tuijian',$tuijian);
		$this->assign('init',$init);
		$this->assign('char',$char);
		$this->assign('imgs',$imgs);
		$this->assign('info',$info);
		return $this->fetch();
	}
	//推荐房源
	public function tuijian(){
		$tui = model('pub')->where('vill='.$this->info['vill'])->select();
		$tuijian = [];
		foreach($tui as $k=>$v){
			$tuijian[] = $v->toArray();
			if($tuijian[$k]['state'] == '合租'){
					$he = model('hezu')->where('rid='.$tuijian[$k]['rid'])->select();
					$hezu = [];
					foreach($he as $kk=>$v){
						$hezu[] = $v->toArray();
						$tuijian[$k]['hid'][] = $hezu[$kk]['hid'];
					}
				}

		}
		return $tuijian;
	}
}
